//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.task.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.base.domain.SystemClass;
import com.ruoyi.live.base.domain.TaskCatalog;
import com.ruoyi.live.base.domain.User;
import com.ruoyi.live.base.mapper.TaskCatalogMapper;
import com.ruoyi.live.base.mapper.UserMapper;
import com.ruoyi.live.tool.commons.IConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommonService {
    @Autowired
    TaskCatalogMapper catalogDao;
    @Autowired
    UserMapper userDao;
    @Autowired
    JdbcTemplate jdbcTemplate;

    public CommonService() {
    }

    public List<TaskCatalog> listCatalog() {
        return this.catalogDao.selectList((Wrapper)(new QueryWrapper((Object)null)).orderByAsc("orders"));
    }

    public List<Map<String, Object>> listTaskState() {
        String[] strs = new String[]{"老师和学生可见", "仅老师可见", "都不可见"};
        List<Map<String, Object>> res = new ArrayList();

        for(int i = 1; i <= 3; ++i) {
            Map<String, Object> map = new HashMap();
            map.put("id", IConvert.int2long(i));
            map.put("name", strs[i - 1]);
            res.add(map);
        }

        return res;
    }

    public List<Map<String, Object>> listSourceType() {
        String[] strs = new String[]{"案例", "资源", "导学"};
        List<Map<String, Object>> res = new ArrayList();

        for(int i = 1; i <= 3; ++i) {
            Map<String, Object> map = new HashMap();
            map.put("id", IConvert.int2long(i));
            map.put("name", strs[i - 1]);
            res.add(map);
        }

        return res;
    }

    public List<Map<String, Object>> listTaskType() {
        List<Map<String, Object>> list = new ArrayList();
        Map<String, Object> res = new HashMap();
        res.put("id", 1);
        res.put("name", "无实训任务");
        list.add(res);
        res = null;
        res = new HashMap();
        res.put("id", 2);
        res.put("name", "有实训任务");
        list.add(res);
        return list;
    }

    public List<User> getTeachers() {
        return this.userDao.selectTeachers();
    }

    public List<Map<String, Object>> listStudent() {
        String sql = "select id,ifnull(user_real_name, user_login_name) name from user where user_type = 3 and class_id = " + SessionInfo.getClassId();
        return this.jdbcTemplate.queryForList(sql);
    }

    public List<Map<String, Object>> radarData(Long id) {
        String sql = "select a.catalog_name name,ifnull(b.avg, 0) avg, ifnull(b.max, 0) max from task_catalog a left join  (select round(avg(score),2) avg,round(max(score)) max,catalog_id from task_user_learn_log where create_user = ? and score_state = 2 group by catalog_id) b on a.id = b.catalog_id ";
        return this.jdbcTemplate.queryForList(sql, new Object[]{id});
    }

    public List<SystemClass> listClass() {
        String sql = "select a.* from system_class a where a.id in (select b.class_id from live_exam_class b)";
        return this.jdbcTemplate.query(sql, new BeanPropertyRowMapper(SystemClass.class));
    }
}
